package com.base.cn.platform.os.service.op.feedback;

import com.base.cn.platform.os.common.msg.WXTmpEnum;
import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.op.feedback.OpFeedbackDao;
import com.base.cn.platform.os.entity.op.feedback.OpFeedback;
import com.base.cn.platform.os.service.op.affiche.OpReceiveAfficheBiz;
import com.base.cn.platform.os.service.op.affiche.tmp.SendMessage;
import com.base.cn.platform.os.service.user.user.CusUserBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户反馈Biz
 * @author zsw
 * @create 2018/4/26
 */
@Service
public class OpFeedbackBiz extends BaseBiz<OpFeedback,OpFeedbackDao> {

    @Autowired
    private OpReceiveAfficheBiz receiveAfficheBiz;
    @Autowired
    private CusUserBiz cusUserBiz;
    @Autowired
    private SendMessage sendMessage;

    /**
     * 查询用户反馈数量
     * @param feedback 反馈实体
     * @return
     */
    public BigDecimal count(OpFeedback feedback){
        return this.count(this.getWhereSql(feedback));
    }

    /**
     * 根据id查询反馈
     *
     * @param feedbackId 反馈id
     * @return
     */
    public OpFeedback findFeedbackById(BigDecimal feedbackId){
        OpFeedback opFeedback = this.findById(feedbackId);
        opFeedback.imageUrlsToImageUrlList();
        return opFeedback;
    }

    /**
     * 查询用户反馈列表
     * @param page 当前页数
     * @param feedback 反馈实体
     * @return
     */
    public PageInfo<OpFeedback> queryFeedbackList(Pagination page,OpFeedback feedback){
        String whereSql = this.getWhereSql(feedback);
        PageInfo<OpFeedback> feedbackList = this.findPage(whereSql, page, null);
        List<OpFeedback> _feedbackList = feedbackList.getList();
        cusUserBiz.setUserData(_feedbackList, true);
        return feedbackList;
    }

    /**
     * 删除用户反馈
     * @param feedbackIds 反馈id串
     */
    public void deleteFeedback(String feedbackIds) {
        this.deleteWhereSql(" id in ("+ feedbackIds +") ");
    }

    /**
     * 保存修改用户反馈
     * @param feedback 反馈实体
     */
    public void saveUpdateFeedback(OpFeedback feedback){
        OpFeedback _feedBack = this.findById(feedback.getId());
        _feedBack.setReplyContext(feedback.getReplyContext());
        _feedBack.setStatus(2);
        this.updateByWhereSql(_feedBack," id=" + feedback.getId());

        //反馈后给前台用户发消息
        Map<BigDecimal,List> dataListMap = new HashMap<>();
        dataListMap.put(_feedBack.getUserId(),Arrays.asList(_feedBack));
        sendMessage.sendMessage(WXTmpEnum.FEEDBACK, Arrays.asList(_feedBack.getUserId()),dataListMap);
    }

      public Map<BigDecimal, OpFeedback> opArticleMapByIds(String feedbackIds){
          List<OpFeedback> opFeedbacks = this.feedbackByIds(feedbackIds);
          return opFeedbacks.stream().collect(Collectors.toMap(OpFeedback::getId, e->e));
    }

    private List<OpFeedback>  feedbackByIds(String  feedbackIds){
        List<OpFeedback> opFeedbacks = this.find("id in(" + feedbackIds + ")", null, null);
        return opFeedbacks;
    }


    /**
     * sql模板
     * @param feedback 查询条件
     * @return
     */
    private String getWhereSql(OpFeedback feedback){
        String whereSql = " 1=1 ";
        if (DataUtil.idIsNotNull(feedback.getUserId())){
            whereSql += " and userId =" + feedback.getUserId();
        }
        if (StringUtils.isNotEmpty(feedback.getContext())) {
            whereSql += " and context like concat('%','" + feedback.getContext() + "','%')";
        }
        if (StringUtils.isNotEmpty(feedback.getFeedBackEmail())){
            whereSql += " and feedBackEmail like '%" + feedback.getFeedBackEmail() +"%'";
        }
        if (StringUtils.isNotEmpty(feedback.getReplyContext())){
            whereSql += " and replyContext like '%" + feedback.getReplyContext() +"%'";
        }
        if (StringUtils.isNotEmpty(feedback.getStartDate())){
            whereSql += " and starDate = "+ feedback.getStartDate();
        }
        if (DataUtil.idIsNotNull(feedback.getCreateUserId())) {
            whereSql += " and createUserId =" + feedback.getCreateUserId();
        }
        if (StringUtils.isNotEmpty(feedback.getCreateTime())) {
            whereSql += " and createTime >='"+ DateUtils.format(feedback.getCreateTime(), "yyyy-MM-dd HH:mm:ss") +"'";
        }
        if (StringUtils.isNotEmpty(feedback.getUser())){
            whereSql += " and userName like concat ('%','" + feedback.getUser() +"','%')";
        }
        if (DataUtil.idIsNotNull(feedback.getId()) && feedback.getId().compareTo(new BigDecimal(0))>0){
            whereSql += " and id in ("+ feedback.getId() +")";
        }
        if (StringUtils.isNotEmpty(feedback.getStatus())){
            whereSql += " and status=" +feedback.getStatus();
        }
         whereSql += " order by createTime desc";
        return whereSql;
    }
}
